package topcoder;

public class AlphabetPath {

	public String doesItExist(String[] letterMaze){
		int i=0,j=0;
		init:
			for (i = 0; i < letterMaze.length; i++) {
				for (j = 0; j < letterMaze[i].length(); j++) {
					if(letterMaze[i].charAt(j) == 'A')
						break init;
				}
			}
		
		char c = letterMaze[i].charAt(j);
		int x=i, y=j;
		path:
			while(c != 'Z'){
				for(i = Math.max(x-1, 0); i <= Math.min(x+1, letterMaze.length-1); i++){
					for(j = Math.max(y-1, 0); j <= Math.min(y+1, letterMaze[i].length()-1); j++){
						if(Math.abs(x-i)+Math.abs(y-j)==1 && (int)letterMaze[i].charAt(j) == (int)(c+1)){
							c = letterMaze[i].charAt(j);
							x=i; y=j;
							continue path;
						}
							
					}
				}
				return "NO";
				
			}
		
		return "YES";
	}
	
	public static void main(String[] args) {
		AlphabetPath alphabetPath = new AlphabetPath();
		
		String[] a = {"ACBDEFGHIJKLMNOPQRSTUVWXYZ"};
		System.out.println(alphabetPath.doesItExist(a));
	}
}
